package com.syq;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.syq.pojo.Person;
import com.syq.utils.EsUtil;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;

/**
 * @ClassName: testDoc
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/8/8 9:22
 */


public class testDoc {
    private String index="person";
    ObjectMapper objectMapper = new ObjectMapper();

    @Before
    public void before(){
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);//属性如果是NULL不进行转换
    }

    @Test
    public void testAddDoc() throws Exception{
        RestHighLevelClient client = EsUtil.getClient();

        Person person = new Person("1","成龙",60,new Date());


        String jsonObject = objectMapper.writeValueAsString(person);

        //索引库的名称
        IndexRequest indexRequest = new IndexRequest(index);
        //ID
        indexRequest.id(person.getId());
        //JSON 文档全部内容
        indexRequest.source(jsonObject, XContentType.JSON);

        //执行请求并响应
        IndexResponse indexResponse = client.index(indexRequest,RequestOptions.DEFAULT);

        System.out.println(indexResponse.status());

    }
    //修改文档  覆盖式修改
    @Test
    public void testDocDoc() throws IOException {
        RestHighLevelClient client = EsUtil.getClient();
        UpdateRequest updateRequest = new UpdateRequest();
        UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);


    }
}
